package com.wrupple.muba.catalogs.client.activity.impl;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Image;
import com.wrupple.muba.catalogs.client.activity.BrowsingActivity;
import com.wrupple.muba.catalogs.client.activity.process.CatalogSelectionProcess;
import com.wrupple.muba.catalogs.client.activity.process.impl.BrowsingActivityProcess;
import com.wrupple.muba.catalogs.client.activity.process.state.BrowsePlaceInterpret;
import com.wrupple.muba.catalogs.client.activity.process.state.InteractionExitState;
import com.wrupple.muba.catalogs.client.module.services.presentation.CatalogPlaceInterpret;
import com.wrupple.muba.catalogs.client.widgets.impl.CatalogUserInterfaceMessages;
import com.wrupple.muba.catalogs.domain.CatalogCreatePlace;
import com.wrupple.muba.catalogs.domain.CatalogEntry;
import com.wrupple.muba.common.client.activity.ActivityProcess;
import com.wrupple.muba.common.client.application.DataCallback;
import com.wrupple.muba.common.domain.DesktopPlace;
import com.wrupple.muba.common.shared.State.DesktopManager;
import com.wrupple.muba.common.shared.State.ProcessManager;
import com.wrupple.muba.common.shared.State.StorageManager;
import com.wrupple.muba.common.shared.StateTransition;

public class SimpleBrowsingActivity extends
		AbstractCatalogActivity<ProcessManager> implements
		BrowsingActivity {
	public class StartDelete implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			StateTransition callback = new DeleteEntries();
			process.startSelection(callback);
		}

	}

	class DeleteEntries extends DataCallback<List<CatalogEntry>> {

		@Override
		public void execute() {
			List<String> ids = new ArrayList<String>(result.size());
			StateTransition<CatalogEntry> callback;
			for (CatalogEntry e : result) {
				ids.add(e.getIdAsString());
				callback = new DeleteCallback(e);
				sm.delete(catalog, e.getIdAsString(), callback);
			}
			
			
		}

	}
	
	class DeleteCallback extends DataCallback<CatalogEntry>{

		private CatalogEntry entryToDelete;

		public DeleteCallback(CatalogEntry e) {
			this.entryToDelete=e;
		}

		@Override
		public void execute() {
			boolean success= result!=null && result.getIdAsString().equals(entryToDelete.getIdAsString());
			if(!success){
				//retry
			}
		}
		
	}
	public class StartCreate implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			dm.goTo(new CatalogCreatePlace(catalog));
		}

	}

	protected BrowsePlaceInterpret read;
	protected InteractionExitState exit;
	protected final CatalogSelectionProcess<?> process;
	protected StorageManager sm;

	// ////////////////////////////////////////////////////////////////////////////
	// ///////////////////////// CLASS STARTS HERE
	// //////////////////////////////
	// ////////////////////////////////////////////////////////////////////////////

	public SimpleBrowsingActivity(
			CatalogSelectionProcess<?> activityProcess,
			ProcessManager delegate,BrowsePlaceInterpret read,InteractionExitState exit,
			CatalogPlaceInterpret placeInterpret,
			DesktopManager dm, StorageManager storageManager) {
		super(delegate, placeInterpret, dm);
		
		this.sm = storageManager;
		this.process = activityProcess;
		this.read=read;
		this.exit=exit;
		read.setPlaceInterpret(placeInterpret);
		process.allowMultipleSelection(false);
	}

	@Override
	public void getActivityProcess(DesktopPlace item,
			final DataCallback<ActivityProcess> callback) {
		
		final ActivityProcess activityProcess =  new  BrowsingActivityProcess(read,process,exit); 
		
		callback.setResult(activityProcess);
		callback.execute();

	}

	public Image getCreateButtonDescriptor(ImageResource imageResource,
			ClickHandler handler, CatalogUserInterfaceMessages constants) {
		String create = constants.create();
		String createTitle = constants.createHelp();
		Image button = new Image(imageResource);
		button.addClickHandler(handler);
		button.setAltText(create);
		button.setTitle(createTitle);
		return button;
	}

	public Image getDeleteButtonDescriptor(ImageResource resource,
			ClickHandler handler, CatalogUserInterfaceMessages constants) {
		String name = constants.delete();
		String help = constants.deleteHelp();
		Image delete = new Image(resource);
		delete.addClickHandler(handler);
		delete.setAltText(name);
		delete.setTitle(help);
		return delete;
	}

}
